package yy.yyds.org.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yy.yyds.basic.query.util.AjaxResult;
import yy.yyds.basic.query.util.QueryResult;
import yy.yyds.org.domain.Employee;
import yy.yyds.org.query.EmployeeQuery;
import yy.yyds.org.service.IEmployeeService;

import java.util.List;

@RestController//ResponseBody+@Controller
@RequestMapping("/employee")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    //@RequestMapping("/querById")
    /*
    * @PathVariable从url地址中取值
    * 此时请求方式：get请求http://localhost:8080/dept/3
     * */
    @GetMapping("/{id}")

    public Employee querById(@PathVariable Long id){
        return employeeService.loadOne(id);
    }
    /*查询所有*/
    @GetMapping
    public List<Employee> querAll(){
        return employeeService.loadAll();
    }

    /*分页查询
    * @RequestBody:从请求体中获取json格式的参数
    * @RequestParam：从请求体或者url路径中获取 username=张三&password=123  键值对格式
    */
    @PostMapping
    public QueryResult<Employee> queryPage(@RequestBody EmployeeQuery query){
        return  employeeService.querypage(query);
    }

    /*根据id删除一条

    * */
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable long id){
        try {
            employeeService.delete(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return   AjaxResult.me().setSuccess(false).setMessage("操作成功");
        }
    }
    /*修该改和新增*/
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }
    /*批量删除*/
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        employeeService.batchDelete(ids);
        try {
            employeeService.batchDelete(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return   AjaxResult.me().setSuccess(false).setMessage("操作成功");
        }
    }


}
